package vip.xiaonuo.integrationconfig.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.integrationconfig.model.ConfigFileInfo;
import vip.xiaonuo.integrationconfig.model.DefaultConfiguration;
import vip.xiaonuo.integrationconfig.param.ConfigQueryParam;
import vip.xiaonuo.integrationconfig.param.ConfigSaveParam;
import vip.xiaonuo.integrationconfig.service.IntegrationService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
public class IntegrationConfigController {

    @Resource
    private IntegrationService service;

    /**
     * 获取所有的配置项
     * @return
     * @throws IOException
     */
    @Permission
    @GetMapping("/integration/list")
    @BusinessLog(title = "集成配置_全部", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData integrationList() throws IOException {
        List<ConfigFileInfo> infos = service.getAllConfigFiles();
        return ResponseData.success(infos);
    }

    /**
     * 根据文件名获取参数内容
     * @return
     * @throws IOException
     */
    @Permission
    @PostMapping("/integration/info")
    @BusinessLog(title = "集成配置_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData integrationInfo(@RequestBody ConfigQueryParam param) throws IOException {
        DefaultConfiguration config = service.getConfig(param.getFileName());
        return ResponseData.success(config);
    }

    /**
     * 保存配置
     * @return
     * @throws IOException
     */
    @Permission
    @PostMapping("/integration/save")
    @BusinessLog(title = "集成配置_保存", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData integrationSave(@RequestBody ConfigSaveParam param) throws IOException {
        service.save(param);
        return ResponseData.success();
    }


}
